﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Globalization;
using System.Web;

namespace SBLib.Common.GlobalizationKS
{
    public class GlobalizationFuncKS
    {
        /// <summary>
        /// Resolve the culture. Browser language
        /// </summary>
        /// <returns></returns>
        public static CultureInfo ResolveCulture()
        {
            string[] languages = HttpContext.Current.Request.UserLanguages;

            if (languages == null || languages.Length == 0)
                return null;

            try
            {
                string language = languages[0].ToLowerInvariant().Trim();
                return CultureInfo.CreateSpecificCulture(language);
            }
            catch (ArgumentException)
            {
                return null;
            }

        }

        /// <summary>
        /// Resolve the country
        /// </summary>
        /// <returns></returns>
        public static RegionInfo ResolveCountry()
        {
            try
            {
                CultureInfo culture = ResolveCulture();
                if (culture != null)
                    return new RegionInfo(culture.LCID);

                return null;
            }
            catch (Exception)
            {

                return null;
            }
        }

        /// <summary>
        /// Get All Countries
        /// </summary>
        /// <returns></returns>
        public static List<string> GetAllCountries()
        {
            try
            {
                System.Collections.Specialized.StringDictionary dic = new System.Collections.Specialized.StringDictionary();
                System.Collections.Generic.List<string> col = new System.Collections.Generic.List<string>();

                foreach (CultureInfo ci in CultureInfo.GetCultures(CultureTypes.AllCultures & ~CultureTypes.NeutralCultures))
                {
                    try
                    {
                        RegionInfo ri = new RegionInfo(ci.LCID);
                        if (!dic.ContainsKey(ri.EnglishName))
                            dic.Add(ri.EnglishName, ri.TwoLetterISORegionName.ToLowerInvariant());

                        if (!col.Contains(ri.EnglishName))
                            col.Add(ri.EnglishName);
                    }
                    catch (Exception)
                    {
                    }
                }
                col.Sort();

                return col;
            }
            catch (Exception)
            {

                return null;
            }

        } 



    }
}
